package com.qingzhuge.file.service.cloud;

import com.qingzhuge.file.entity.FileStorageConfig;
import com.qingzhuge.file.service.BaseFileStorageService;

import java.io.*;
import java.util.Objects;

/**
 * @author : zero.xiao
 * @description : 本地存储
 * @date :2018/6/8 0008 下午 15:13
 * @modified :
 * @since : 1.0.0
 */
public class StorageLocalService extends BaseFileStorageService {

    StorageLocalService(FileStorageConfig config) {
        this.config = config;
    }

    @Override
    protected String separator() {
        return FILE_SEPARATOR;
    }

    @Override
    protected String getPrefix() {
        return config.getLocalPrefix();
    }

    @Override
    public String upload(byte[] data, String path) {
        File file = createFile(path);
        BufferedOutputStream out = null;
        try {
            out = new BufferedOutputStream(new FileOutputStream(file));
            out.write(data);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                Objects.requireNonNull(out).close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return path;
    }

    @Override
    public String upload(InputStream inputStream, String path) {
        File file = createFile(path);
        BufferedInputStream in = null;
        BufferedOutputStream out = null;
        try {
            in = new BufferedInputStream(inputStream);
            out = new BufferedOutputStream(new FileOutputStream(file));
            //读取文件的数据
            int l;
            //声明该变量用于存储读取到的数据
            byte[] buf = new byte[1024];
            while ((l = in.read(buf)) != -1) {
                out.write(buf, 0, l);
                out.flush();
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            try {
                Objects.requireNonNull(in).close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                Objects.requireNonNull(out).close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return path;
    }

    @Override
    public void download(String path, OutputStream outputStream) {
        File file = new File(path);
        if (file.getParentFile().exists() && file.exists() && file.isFile()) {
            InputStream in = null;
            try {
                in = new FileInputStream(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            OutputStream out = new BufferedOutputStream(outputStream);
            writeStream(in, out);
        }
    }

    @Override
    public void delete(String path) {
        File file = new File(path);
        if (file.exists()) {
            boolean delete = file.delete();
            if (log.isDebugEnabled()) {
                log.debug("文件:[{}]删除:[{}]", path, delete);
            }
        }
    }

    private File createFile(String path) {
        File file = new File(path);
        if (!file.getParentFile().isDirectory()) {
            boolean mkdirs = file.getParentFile().mkdirs();
            log.debug("File create {} :{}", mkdirs, path);
        }
        return file;
    }
}
